﻿using NoteSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NoteSystem
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 初始化省份数据
                InitProvince();
            }
            

        }
        private void InitProvince()
        {
            // 从数据库或其他数据源获取省份数据
            DataSet ds = DbHelper.Query("select * from S_Province"); // 伪代码
            ddlProvince.DataSource = ds;
            ddlProvince.DataTextField = "ProvinceName";
            ddlProvince.DataValueField = "ProvinceID";
            ddlProvince.DataBind();
            ddlProvince.Items.Insert(0, new ListItem("请选择省份", "0"));
        }
        public void InitCity(int ProvinceID)
        {
            // 从数据库或其他数据源获取城市数据
            DataSet ds = DbHelper.Query("select * from S_City where ProvinceID="+ ProvinceID);
            ddlCity.DataSource = ds;
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";
            ddlCity.DataBind();
            ddlCity.Items.Insert(0, new ListItem("请选择城市", "0"));
        }

        private void InitDistrict(int CityID)
        {
            // 从数据库或其他数据源获取区域数据
            DataSet ds = DbHelper.Query("select * from S_District where CityID="+ CityID); // 伪代码
            ddlDistrict.DataSource = ds;
            ddlDistrict.DataTextField = "DistrictName";
            ddlDistrict.DataValueField = "DistrictID";
            ddlDistrict.DataBind();
            ddlDistrict.Items.Insert(0, new ListItem("请选择区域", "0"));
        }
        //注册
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            //获取用户插入的有效信息
            string account = txtAccount.Text.Trim();
            string birthday=txtBirthday.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            int districtId = Convert.ToInt32(ddlDistrict.SelectedValue);//获取下拉框的选中值
            int gender = Convert.ToInt32(rbtnGender.SelectedValue);
            string imgUrl = Image1.ImageUrl;//获取图片框的网络地址
            string phone = txtPhone.Text.Trim();
            string email = txtEmail.Text.Trim();
            string qq=txtQQ.Text.Trim();
            //数据库插入
            string sql = string.Format(@"insert into users values('{0}','{1}','{2}',{3},{4},'{5}','{6}','{7}','{8}',0,0,'{9}',0)", account, birthday, pwd, districtId, gender, imgUrl, phone, email, qq, DateTime.Now);
            //执行更新操作
            int flag = DbHelper.ExecuteSql(sql);
            if (flag > 0)
            {
                Response.Write("<script>alert('注册成功，请等待管理人员审批')</script>");
            }
        }
        //头像上传
        protected void Button1_Click(object sender, EventArgs e)
        {
            string exList = ".jpg.png.gif";
            // \反斜杠:1)转义   2)路径深度（物理）
            // /正斜杠:1)表示网络路径深度
            // 网络路径：网站中使用的路径  图片加载 文件的下载
            // 物理路径：计算机中磁盘路径  本地计算机中硬盘
            //1.捕获文件名称
            string imgName = fulImg.FileName;//a.jpg
            string exName = imgName.Substring(imgName.LastIndexOf('.'));
            if (!exList.Contains(exName))
            {
                Response.Write("<script>alert('支持上传图片格式！')</script>");
                return;
            }
            if (fulImg.FileContent.Length > 2 * 1024 * 1024)
            {
                Response.Write("<script>alert('图片大小不能超过2M！')</script>");
                return;
            }
            //2.确定文件保存的物理地址
            string path = Server.MapPath("images") + "\\" + imgName;
            //3.进行文件保存了
            fulImg.SaveAs(path);
            Image1.ImageUrl = "images/" + imgName;
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCity.Items.Clear();
            if (ddlProvince.SelectedValue != "")
            {
                int ProvinceID = Convert.ToInt32(ddlProvince.SelectedValue);
                InitCity(ProvinceID);
            }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDistrict.Items.Clear();
            if (ddlCity.SelectedValue != "")
            {
                int CityID = Convert.ToInt32(ddlCity.SelectedValue);
                InitDistrict(CityID);
            }
        }
    }
}